﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Objects;
using BAL;
using System.Collections.Generic;

public partial class Admin_UCControls_UCChiNhanh_CapNhat : System.Web.UI.UserControl
{
    public static ChiNhanh ItemChiNhanh;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
         
            GetTinhThanh();

            ItemChiNhanh = new ChiNhanh();
            ItemChiNhanh = (ChiNhanh)Session["ItemChiNhanh"];

            if (ItemChiNhanh != null)
            {
                txtTenChiNhanh.Text = ItemChiNhanh.Ten.ToString();
                txtSoDienThoai.Text = ItemChiNhanh.SoDienThoai.ToString();
                txtDiaChi.Text = ItemChiNhanh.DiaChi.ToString();

                ddlTinhThanh.SelectedIndex = ItemChiNhanh.IdTinhThanh ;
            }
        }
    }

    protected void btnLuu_Click(object sender, EventArgs e)
    {
        if (CheckNull())
        {

            int item = 0;

            if (ItemChiNhanh == null)
            {
                item = BALChiNhanh.Insert_ChiNhanh(txtTenChiNhanh.Text, txtDiaChi.Text, txtSoDienThoai.Text, Convert.ToInt16(ddlTinhThanh.SelectedValue), 0);

            }
            else
            {
                item = BALChiNhanh.Update_ChiNhanh(Int32.Parse(ItemChiNhanh.Id.ToString()), txtTenChiNhanh.Text, txtDiaChi.Text, txtSoDienThoai.Text, ddlTinhThanh.SelectedIndex, 1, Int32.Parse(ItemChiNhanh.SapXep.ToString()));

            }

            if (item > 0)
            {
                lblThongbao.Visible = true;
                lblThongbao.Text = "Cập nhật thành công";

            }
            else
            {
                lblThongbao.Visible = true;
                lblThongbao.Text = "Cập nhật thất bại";
            }
        }
        else
        {
            lblThongbao.Visible = true;
            lblThongbao.Text = "Thông tin chi nhánh chưa đầy đủ";
        }
    }


    private void GetTinhThanh()
    {
        List<TinhThanh> ListTinhThanh = new List<TinhThanh>();
        ListTinhThanh = BAL.BALTinhThanh.GetAllItem();

        TinhThanh item = new TinhThanh();

        item.Id = 0;
        item.TenTinhThanh = "Chọn Tỉnh Thành";

        ListTinhThanh.Insert(0, item);

        ddlTinhThanh.DataTextField = "TenTinhThanh";
        ddlTinhThanh.DataValueField = "Id";
        ddlTinhThanh.DataSource = ListTinhThanh;
        ddlTinhThanh.DataBind();

    }

    protected void btnQuaylai_Click(object sender, EventArgs e)
    {
        Response.Redirect("../Admin/ChiNhanh.aspx");
    }

    public bool CheckNull()
    {
        bool flag = true;
        VisibleLable();
        if (txtTenChiNhanh.Text == "")
        {
            lblTenChiNhanh.Visible = true;
            txtTenChiNhanh.Focus();
            flag = false;
        }

        if (txtDiaChi.Text == "")
        {
            lblDiaChi.Visible = true;
            txtDiaChi.Focus();
            flag = false;
        }

        if (txtSoDienThoai.Text == "")
        {
            lblSoDienThoai.Visible = true;
            txtSoDienThoai.Focus();
            flag = false;
        }

        if (ddlTinhThanh.SelectedIndex == 0)
        {
            lblTinhThanh.Visible = true;
            ddlTinhThanh.Focus();
            flag = false;
        }
        return flag;
    }

    public void VisibleLable()
    {
        lblTenChiNhanh.Visible = false;
        lblTinhThanh.Visible = false;
        lblDiaChi.Visible = false;
        lblSoDienThoai.Visible = false;
    }
    
}
